﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WinFormsApp1.entity;
using WinFormsApp1.utils;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace WinFormsApp1
{
    public partial class FontWindow : Form
    {
        //private DragMoveUtils dragMoveUtils = new DragMoveUtils();
        public FontWindow()
        {
            InitializeComponent();
            //dragMoveUtils.AddControl(NumberLabel);

            UpdateView(new Setting());
        }

        private void DoLock()
        {
            //窗口置底
            User32.SetWindowPos(this.Handle, User32.HWND_NOTOPMOST, 0, 0, 0, 0, User32.SWP_NOMOVE | User32.SWP_NOSIZE);
            // 设置鼠标穿透
            User32.SetWindowLong(this.Handle, User32.GWL_EXSTYLE, User32.WS_EX_TRANSPARENT | User32.WS_EX_LAYERED);
            // 设置背景颜色的，100为透明，现在为23
            //User32.SetLayeredWindowAttributes(this.Handle, 0, 23, User32.LWA_ALPHA);
        }

        /// <summary>
        /// 更新页面
        /// </summary>
        /// <param name="setting"></param>
        public void UpdateView(Setting setting)
        {
            // 计算时间
            DateTime lastTime = StringToDateTime(setting.DateTime);
            int dayNumber = DateDiff(DateTime.Now, lastTime);

            // 替换文字
            setting.Title = setting.Title.Replace("{}", dayNumber.ToString());

            // 计算背景颜色，近似色
            Color color = ColorUtils.HexToApproximateColor(setting.FontColor);
            this.TransparencyKey = color;
            this.BackColor = color;

            Color fontColor = ColorTranslator.FromHtml(setting.FontColor);
            NumberLabel.ForeColor = fontColor;
            NumberLabel.Text = setting.Title;
            NumberLabel.Font = new Font("微软雅黑", setting.FontSize, FontStyle.Bold);

            this.Width = NumberLabel.Width + 5;
            this.Height = NumberLabel.Height + 5;

            this.Location = new Point(setting.LocationX, setting.LocationY);
            IsLock = setting.IsLock;
            if (setting.IsLock)
            {
                DoLock();
            }
            else
            {
                this.FormBorderStyle = FormBorderStyle.None;
            }
        }

        /// <summary>
        /// 字符串转日期
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        private DateTime StringToDateTime(string date)
        {
            DateTimeFormatInfo dtFormat = new DateTimeFormatInfo();
            dtFormat.ShortDatePattern = "yyyy-MM-dd";
            return Convert.ToDateTime(date, dtFormat);
        }

        /// <summary>
        /// 计算相隔天数
        /// </summary>
        /// <param name="dateStart"></param>
        /// <param name="dateEnd"></param>
        /// <returns></returns>
        private int DateDiff(DateTime dateStart, DateTime dateEnd)
        {
            DateTime start = Convert.ToDateTime(dateStart.ToShortDateString());
            DateTime end = Convert.ToDateTime(dateEnd.ToShortDateString());
            TimeSpan sp = end.Subtract(start);
            return sp.Days;
        }

        private Point mouseLocation;//表示鼠标对于窗口左上角的坐标的负数
        private bool isDragging;//标识鼠标是否按下
        public bool IsLock { get; set; } = false; // 是否锁定

        private void FontWIndow_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left && !IsLock)
            {
                mouseLocation = new Point(-e.X, -e.Y);
                //表示鼠标当前位置相对于窗口左上角的坐标，
                //并取负数,这里的e是参数，
                //可以获取鼠标位置
                isDragging = true;//标识鼠标已经按下
            }
        }

        private void FontWIndow_MouseMove(object sender, MouseEventArgs e)
        {
            if (isDragging && !IsLock)
            {
                Point newMouseLocation = MousePosition;
                //获取鼠标当前位置
                newMouseLocation.Offset(mouseLocation.X, mouseLocation.Y);
                //用鼠标当前位置加上鼠标相较于窗体左上角的
                //坐标的负数，也就获取到了新的窗体左上角位置
                Location = newMouseLocation;//设置新的窗体左上角位置
            }
        }

        private void FontWIndow_MouseUp(object sender, MouseEventArgs e)
        {
            if (isDragging && !IsLock)
            {
                isDragging = false;//鼠标已抬起，标识为false
                // 更新输入框中的数值
                Setting setting = SettingWindow.SettingWindowData.GetWindowSetting();
                if (setting != null)
                {
                    setting.LocationX = Location.X;
                    setting.LocationY = Location.Y;
                    DbUtils.SaveSetting(setting);
                    SettingWindow.SettingWindowData.SetWindowSetting(setting);
                    UpdateView(setting);
                }
            }
        }

        private void FontWindow_VisibleChanged(object sender, EventArgs e)
        {
            if (!Visible)
            {
                this.Visible = true;
            }
        }

        private void FontWindow_MinimumSizeChanged(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Normal;
        }

    }
}
